<template>
	<view class="warp">
		<view v-if="userType === 1 ">
			<view class="block" v-show="current === 0">
				<home ref="home1":city="city" @search="search"></home>
			</view>
			<view class="block" v-show="current === 1">
				<shop  ref="shop1"></shop>
			</view>
			<view class="block" v-show="current === 2">
				<guarantee  ref="guarantee"></guarantee>
			</view>
			<view class="block" v-show="current === 3">
				<order></order>
			</view>
			<view class="block" v-show="current === 4">
				<user :userInfo ='userInfo' ref="user"></user>
			</view>
		</view>
		<view v-if="userType === 2 ">
			<view class="block" v-show="current === 0">
				<homesf></homesf>
			</view>
			<view class="block" v-show="current === 1">
				<shop></shop>
			</view>
			<view class="block" v-show="current === 2">
				<mine :userInfo ='userInfo' ref="mine"></mine>
			</view>

		</view>
		<view class="footer_tabbar">
			<tui-tabbar :tabBar="tabBar" color="#7A7E83" selected-color="#477678" background-color="#FFFFFF"
				:isFixed="true" :current="current" @click="tabbarSwitch"></tui-tabbar>
		</view>
	</view>
</template>

<script>
	import guarantee from '../../compontents/yh/guarantee/guarantee.vue'
	import home from '../../compontents/yh/home/index.vue'
	import shop from '../../compontents/yh/shop/index.vue'
	import mine from '../../compontents/sf/user/index.vue'
	import user from '../../compontents/yh/user/index.vue'
	import order from '../../compontents/yh/order/index.vue'
	import homesf from '../../compontents/sf/home/index.vue'
	import tuiTabbar from '@/compontents/tui-tabbar.vue';
	import {
		addressResolution,
		get_advertisement
	} from '../../api/api.js'
	export default {
		components: {
			guarantee,
			home,
			shop,
			user,
			order,
			tuiTabbar,
			homesf,
			mine
		},
		data() {
			return {
				userInfo:{},
				userType:'',
				current: 0,
				city: "",
				tabList: ["力高商厨", "配件商城","一键报修" ,"订单管理", "我的", ],
				tabList1: ["订单管理","配件商城", "我的", ],
			}
		},
		
		computed: {
			tabBar() {
				// 针对两端使用 1可以认为用户端 2 师傅端
				if (this.userType === 1) {
					return [{
						"pagePath": "pages/index/index",
						"iconPath": "/static/1-01.png",
						"selectedIconPath": "/static/1-02.png",
						"text": "首页"
					}, {
						"pagePath": "pages/psychology/index",
						"iconPath": "/static/2-01.png",
						"selectedIconPath": "/static/2-02.png",
						"text": "配件商城"
					}, {
						"pagePath": "pages/guarantee/guarantee",
						"iconPath": "/static/guarantee.png",
						"selectedIconPath": "/static/guarantee.png",
						"text": "一键报修",
						"hump": true
					}
					, {
						"pagePath": "pages/education/index",
						"iconPath": "/static/3-01.png",
						"selectedIconPath": "/static/3-02.png",
						"text": "订单管理"
					}, {
						"pagePath": "pages/user/index",
						"iconPath": "/static/4-01.png",
						"selectedIconPath": "/static/4-02.png",
						"text": "我的"
					}]
				}
				return [{
					"pagePath": "pages/index/index",
					"iconPath": "/static/1-01.png",
					"selectedIconPath": "/static/1-02.png",
					"text": "订单管理"
				},
				{
					"pagePath": "pages/psychology/index",
					"iconPath": "/static/2-01.png",
					"selectedIconPath": "/static/2-02.png",
					"text": "配件商城"
				},{
					"pagePath": "pages/user/index",
					"iconPath": "/static/4-01.png",
					"selectedIconPath": "/static/4-02.png",
					"text": "个人中心"
				}]
			}
		},

		onShow(options) {
			// this.getLocat()
			
			let userInfo = wx.getStorageSync('userInfo')
			this.userInfo = userInfo
			console.log('更新',userInfo)
			this.getAdv()
			let type =uni.getStorageSync('userType')
			if(!type){
				
			}else{
					this.userType = type
			}
		
			// if (!this.userType) {
			// 	uni.navigateTo({
			// 		url: '/pages/login/index'
			// 	})
			// }
			this.current = uni.getStorageSync('current') || 0
			let data = wx.getStorageSync('token')

			console.log('data', data, userInfo)
			if (data && userInfo) {
				uni.setStorageSync('login', true)
				console.log('静默登录成功!')
			} else {
				uni.setStorageSync('login', false)
				console.log('静默登录失败!')
				// uni.navigateTo({
				// 	url: '/pages/login/index'
				// })
			}
			if (this.userType == 1) {
				console.log('currt', this.current)
				wx.setNavigationBarTitle({
					title: this.tabList[this.current]
				})
			}
			if (this.userType == 2) {
				wx.setNavigationBarTitle({
					title: this.tabList1[this.current]
				})
			}
			
				// let price = userInfo.unit_price
				// console.log('执行',price)
				// this.$nextTick(()=>{
				// 	this.$refs.main.userInfo.unit_price = price
				// })
		},

		methods: {
			// 获取广告位
			getAdv(){
				console.log('获取广告位')
				get_advertisement().then(res=>{
					uni.setStorageSync('advList',res.data) 
				})
			},
			search(e){
					console.log('currten',e)
					console.log(this.$refs.home1.keyword)
					this.current = e
				this.$refs.shop1.keyword1 =  this.$refs.home1.keyword
					this.$refs.shop1.query(this.$refs.home1.keyword)
					
			},
			// /* 获取当前位置 */
			// getLocat(){
			// 	addressResolution().then(res=>{
					
			// 	})
			// },
			// 底部tabBar 切换
			tabbarSwitch(tab) {
				console.log(tab)
				if (this.userType == 1) {
					wx.setNavigationBarTitle({
						title: this.tabList[tab.index]
					})
				}
				if (this.userType == 2) {
					wx.setNavigationBarTitle({
						title: this.tabList1[tab.index]
					})
				}
				uni.showLoading({
					title: "加载中",
					duration: 500
				})
				const _this = this;
				_this.is_tab_num = 1;
				_this.currentTab = tab;
				if (tab.index == 0) {
					_this.show_mn = false;
					uni.setStorageSync('current', 0); // 主页
				}
				if (tab.index == 1) {
					_this.show_mn = true;

					uni.setStorageSync('current', 1); // 商城
				}
				if (tab.index == 2) {
					_this.show_mn = true;
					uni.setStorageSync('current', 2); // 一键报修
				}
				if (tab.index == 3) {
					_this.show_mn = true;
					uni.setStorageSync('current', 3); // 发现
				}
				if (tab.index == 4) {
					_this.show_mn = false;
					uni.setStorageSync('current', 4); // 个人中心
				}
				if (_this.current !== tab.index) {
					_this.current = tab.index;
					if (_this.userType === 1) {
						uni.pageScrollTo({
							duration: 0,
							scrollTop: 0
						});
						// uni.$off('Tabbar')
						uni.$emit('token', uni.getStorageSync('token'));
						uni.$emit('changeTabbar', tab);
					}
				}
			}
		}

	}
</script>


<style scoped lang="less">
	.block {
		width: 100%;
		height: 100%;
		overflow-x: hidden;
	}
</style>
